home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / tool / mmlc / source / m2main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-29  |  5.7 KB  |  278 lines

  1. /* ------------------------------------------------------------------
  2.                         MML compiler [M2]
  3.                programmed by S.Yamamoto (SHINNOSUKE)
  4.                     m2main.c   --  main function
  5. ------------------------------------------------------------------ */
  6. /*
  7.  *  (c) 1994-1995 S.Yamamoto
  8.  *
  9.  *  1994,07/01 [M] Ver 1.0
  10.  *  1994,10/02 Version 1.0
  11.  *  1994,12/13 Version 2.0
  12.  *  1995,02/10 Version 2.1
  13.  *  1995,02/19 Version 2.2
  14.  *  1995,03/11 Version 2.3
  15.  *  1995,03/17 Version 2.4
  16.  *  1995,03/25 Version 2.5
  17.  *  1995,06/09 Version 2.6
  18.  *  1995,07/29 Version 2.7
  19.  */
  20.  
  21. #include    <stdio.h>
  22. #include    <stdlib.h>
  23. #include    <ctype.h>
  24. #include    <string.h>
  25. #include    <stdarg.h>
  26. #include    <limits.h>
  27. #include    "m2.h"
  28.  
  29. #define    SUCCSESS    0
  30. #define    FAILURE    (-1)
  31. #define    TRUE    1
  32. #define    FALSE    0
  33. #define    ERR    (-1)
  34. #define    YES    1
  35. #define    NO    0
  36.  
  37. #define    CAT    0
  38. #define    CHG    1
  39.  
  40. char    *msgTTL =
  41.     "\n"
  42.     "This is MML compiler for SMF on MS-DOS.\n"
  43.     "\n"
  44.     "Usage :M2 [-option] <file name>\n"
  45.     "Option:-o -r -n -s -p -h\n"
  46.     "Ex 1  :M2 sample.m2\n"
  47.     "EX 2  :M2 -o -r sample.mid sample.m2\n";
  48.  
  49. char    *msgTTL2 =
  50.     "\nMML compiler [M2] ver 2.7 -- (c)SHINNOSUKE 1995\n";
  51.  
  52. FILE    *fpo;
  53. unsigned    long    dataSize = 0;
  54. unsigned    int    workSize = 3000;
  55. int    dispFlag = TRUE;
  56.  
  57.  
  58. void    putDisp( char *format ,... )    /* 画面表示 */
  59. {
  60.     va_list    args;
  61.     va_start( args ,format );
  62.     if( dispFlag == TRUE )
  63.         vprintf( format ,args );
  64.     va_end( args );
  65.     return;
  66. }
  67.  
  68. int    putData(int argc,...)    /* データ出力 */
  69. {
  70.     int    d[32];
  71.     int    i;
  72.     int    n = 0;
  73.  
  74.     va_list    ap;
  75.     va_start( ap ,argc );
  76.  
  77.     while( n < argc ) {
  78.         d[n] = va_arg( ap ,int );
  79.         n++;
  80.     }
  81.     va_end( ap );
  82.     for( i=0;i<argc;i++ )
  83.         fputc( d[i] ,fpo );
  84.     dataSize = dataSize + argc;
  85.  
  86.     return(SUCCSESS);
  87. }
  88.  
  89. void    sizeWrite( void )    /* データ長を書き込む */
  90. {
  91.     int    d1;
  92.     int    d2;
  93.     int    d3;
  94.     int    d4;
  95.  
  96.     putData( 4 ,0x00 ,0xff ,0x2f ,0x00);    /* end of track */
  97.     dataSize -= 22;    /* ヘッダ長をひく */
  98.  
  99.     d1 = (int) (( dataSize&0xff000000 ) >> 24 );
  100.     d2 = (int) (( dataSize&0xff0000 ) >> 16 );
  101.     d3 = (int) (( dataSize&0xff00 ) >> 8 );
  102.     d4 = (int) ( dataSize&0xff );
  103.  
  104.     fseek( fpo ,18L ,SEEK_SET );
  105.     fputc( d1 ,fpo );
  106.     fputc( d2 ,fpo );
  107.     fputc( d3 ,fpo );
  108.     fputc( d4 ,fpo );
  109.  
  110.     return;
  111. }
  112.  
  113. void    title(void)    /* タイトル表示 */
  114. {
  115.     printf( msgTTL );
  116.     return;
  117. }
  118.  
  119. int    fNameCat( int mode ,char *f ,char *s)    /* 拡張子の変換 */
  120. {
  121.     int    i;
  122.     char    *f2;
  123.     char    *f3;
  124.  
  125.     f2 = strrchr( f ,'\\' );
  126.     if( f2 == NULL )    f2 = f;
  127.  
  128.     f3 = strrchr( f2 ,'.' );
  129.     if( f3 == NULL ) {
  130.         strcat( f ,s );
  131.         return( SUCCSESS );
  132.     }
  133.  
  134.     if( f3 != NULL && mode == CAT )
  135.         return( SUCCSESS );
  136.  
  137.     for( i=0;i<=strlen(s);i++ )
  138.         f3[i] = s[i];
  139.  
  140.     return(SUCCSESS);
  141. }
  142. int main(int argc,char *argv[]) /* メイン */
  143. {
  144.     char    ifn[FILENAME_MAX];
  145.     char    ofn[FILENAME_MAX];
  146.  
  147.     char    *s;
  148.  
  149.     int    i;
  150.     int    j;
  151.     int    arg = argc;
  152.     int    oof = FALSE;
  153.     int    orf = 0;
  154.     int    pre = FALSE;
  155.  
  156.     unsigned long    alcSize;
  157.  
  158.     FLGDAT    fgset;
  159.     WORKMEM    *work;
  160.  
  161.     if( argc <= 1 ) {
  162.         printf( msgTTL2 );
  163.         printf( "Argument is not correct.\n" );
  164.         title();
  165.         exit( EXIT_FAILURE );
  166.     }
  167.  
  168.     for( i=1;i<argc;i++ ) {
  169.         if( argv[i][0]!='-' ) {
  170.             arg = i;
  171.             break;
  172.         }
  173.         for( j=1;j<255;j++ ) {
  174.             if( argv[i][j] == '\0' )    break;
  175.             switch( toupper( argv[i][j] )) {
  176.             case 'O':    /* set output file */
  177.                 oof = TRUE;
  178.                 break;
  179.             case 'R':    /* use running status */
  180.                 orf = orf | 1;
  181.                 break;
  182.             case 'S':    /* shift status */
  183.                 orf = orf | 2;
  184.                 break;
  185.             case 'H':    /* Hi-speed compile */
  186.                 orf = orf | 4;
  187.                 break;
  188.             case 'C':    /* check data */
  189.                 orf = orf | 8;
  190.                 break;
  191.             case 'N':    /* no print */
  192.                 dispFlag = FALSE;
  193.                 break;
  194.             case 'P':    /* pre-process only */
  195.                 pre = TRUE;
  196.                 break;
  197.             default:
  198.                 printf( msgTTL2) ;
  199.                 printf( "Illgel option '-%c'\n" ,argv[i][j] );
  200.                 title();
  201.                 exit( EXIT_FAILURE );
  202.             }
  203.         }
  204.     }
  205.  
  206.     if(( argc==arg ) || (( oof == TRUE) && ( arg>argc-2 ))) {
  207.         printf( msgTTL2 );
  208.         printf( "Argument is not correct.\n" );
  209.         title();
  210.         exit( EXIT_FAILURE );
  211.     }
  212.  
  213.     putDisp( msgTTL2 );
  214.  
  215.     if( oof == TRUE ) {
  216.         strcpy( ifn ,argv[arg+1] );
  217.         strcpy( ofn ,argv[arg] );
  218.         fNameCat( CAT ,ifn ,".m2" );
  219.     }
  220.     else {
  221.         strcpy( ifn ,argv[arg] );
  222.         strcpy( ofn ,argv[arg] );
  223.         fNameCat( CAT ,ifn ,".m2" );
  224.         if( pre == FALSE )
  225.             fNameCat( CHG ,ofn ,".mid" );
  226.         else    fNameCat( CHG ,ofn ,".mp" );
  227.     }
  228.  
  229.     if( Pre_open( ifn ) == ERR ) {
  230.         printf( "%s ... Can't open file\n" ,ifn );
  231.         exit( EXIT_FAILURE );
  232.     }
  233.  
  234.     if( pre == TRUE ) {
  235.         if(( fpo=fopen( ofn ,"w" )) == NULL ) {
  236.             printf( "%s ... Can't create file\n" ,ofn );
  237.             exit( EXIT_FAILURE );
  238.         }
  239.         while(( s=Pre_fgets() ) != NULL )
  240.             fprintf( fpo ,"%s\n" ,s );
  241.         putDisp( "***** pre-process completed *****\n" );
  242.         exit( EXIT_SUCCESS );
  243.     }
  244.  
  245.     if(( fpo=fopen( ofn ,"wb" )) == NULL ) {
  246.         printf( "%s ... Can't create file\n" ,ofn );
  247.         exit( EXIT_FAILURE );
  248.     }
  249.  
  250.     putDisp( " %s -> %s\n" ,ifn ,ofn );
  251.     putDisp( "--------------------------------------------------\n" );
  252.  
  253.     fgset = ctrlLine();
  254.     alcSize = fgset.wksize * sizeof(WORKMEM);
  255.     if( alcSize > UINT_MAX ) {
  256.         printf( "Can't allocate Working area for MIDI event. (.malloc/max %u)\n" ,
  257.             UINT_MAX / sizeof( WORKMEM ) );
  258.         exit( EXIT_FAILURE );
  259.     }
  260.     if(( work=malloc(( unsigned int ) alcSize )) == NULL ) {
  261.         printf( "Can't allocate memory(%lu bytes) (.malloc)\n" ,alcSize );
  262.         exit( EXIT_FAILURE );
  263.     }
  264.  
  265.     putDisp( "Working area  :%ld (byte)\n" ,alcSize );
  266.     putDisp( "--------------------------------------------------\n" );
  267.  
  268.     mmlLine( fgset ,work ,orf );
  269.     sizeWrite();
  270.     fclose( fpo );
  271.  
  272.     putDisp( "--------------------------------------------------\n" );
  273.     putDisp( "File size     :%ld\n" ,dataSize + 22L );
  274.     putDisp( "***** compile completed *****\n" );
  275.     return( SUCCSESS );
  276. }
  277.  
  278.